//
//  Module2ViewController.m
//  BlueToothProject
//
//  Created by Lhz_Mac on 2017/3/16.
//  Copyright © 2017年 yxw. All rights reserved.
//

#import "Module2ViewController.h"
#import "BlueToothService.h"
#import "BabyToy.h"
#import "ColorPickerViewController.h"
#import "SVProgressHUD.h"
#import "UIImage+Color.h"

@interface Module2ViewController ()
@property (strong,nonatomic) ColorPickerViewController * colorChooseVc;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong,nonatomic) BlueToothService *btService;
@end

@implementation Module2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"模块2";
    
    self.btService = [BlueToothService sharedDefaultManager];
    
    
}

- (IBAction)seleceColorAction:(id)sender {
    
    self.colorChooseVc.chooseColor = self.imageView.backgroundColor;
    [self.navigationController pushViewController:self.colorChooseVc animated:YES];
}


-(IBAction)bluetoothSend:(id)send{
    
//    if (!self.btService.isConnectSuccess) {
//        [SVProgressHUD showInfoWithStatus:@"请先连接设备"];
//        return;
//    }
    
    // 将背景图的背景颜色转换成image,然后才可以通过蓝牙发送出去
    [self.btService transferPhotoWithImage:[UIImage imageWithColor:self.colorChooseVc.chooseColor]];
    
}

//匹配硬件控制效果
- (IBAction)matchUp:(UIButton *)sender {
    
    if (!self.btService.isConnectSuccess) {
        [SVProgressHUD showInfoWithStatus:@"请先连接设备"];
        return;
    }
    
    NSInteger btnTag = sender.tag;
    
    if (btnTag > 0) {
       [self.btService sendCommandToBlueTooth:(int)btnTag];
    }else{
        [SVProgressHUD showInfoWithStatus:@"发送指令为空！"];
    }
    
}

#pragma mark - lazyloading
-(ColorPickerViewController *)colorChooseVc
{
    if (!_colorChooseVc) {
        _colorChooseVc = [[ColorPickerViewController alloc]init];
        [_colorChooseVc addObserver:self forKeyPath:@"chooseColor" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    }
    return _colorChooseVc;
}

#pragma mark - KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"chooseColor"]) {
        UIImage * image = [UIImage imageWithColor:self.colorChooseVc.chooseColor];
        self.imageView.image = image;
    }
}

-(void)dealloc
{
    [_colorChooseVc removeObserver:self forKeyPath:@"chooseColor"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
